/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.world.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import net.javlov.world.Body;
import net.javlov.world.phys2d.Phys2DBody;

public class BodyView {

	protected Body model;
	
	public BodyView() {}
	
	public BodyView(Body model) {
		setModel(model);
	}

	///// GETTERS & SETTERS ///////
	public Body getModel() {
		return model;
	}

	public void setModel(Body model) {
		this.model = model;
	}
	
	///// DRAWING METHODS /////
	public void draw(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		java.awt.Shape txShape;
		AffineTransform at = getBodyTransform();
		txShape = at.createTransformedShape(model.getFigure());
		
		//TODO: cast doesn't belong here, BodyView should be for Body, need Phys2DBodyView
		//g2d.setColor( ((Phys2DBody) model).getShape().getColor() );
		g2d.draw( model.getFigure() );
		
		//draw bounding box
		//g2d.setColor(Color.green);
		//g2d.draw( txShape.getBounds2D() );
	}
	
	/**
	 * Creates a transform representing the body's current translation and rotation, in order to
	 * be able to draw it.
	 * 
	 * @return a transform representing the body's current translation and rotation
	 */
	protected AffineTransform getBodyTransform() {
		Point2D pos = model.getLocation();
		AffineTransform transform = new AffineTransform();
		//rotate around the body's center
		transform.rotate(model.getBearing(), pos.getX(), pos.getY());
		//translate to Java coordinates: Phys2D position is at center, Java at top left
		//transform.translate(pos.getX(), pos.getY());
		return transform;
	}
}
